﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using ZFB.Common;
using ZFB.IServices;

namespace ZFB.WebAPI.Controllers
{
    /// <summary>
    /// 角色
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RolesController : ControllerBase
    {
        private readonly IRoleServices roleServices;

        public RolesController(IRoleServices roleServices)
        {
            this.roleServices = roleServices;
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddNew(string roleName) 
        {
            return await roleServices.AddNewAsync(roleName);
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> Update(long roleId, string roleName) 
        {
            return await roleServices.UpdateAsync(roleId, roleName);
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> MarkDeleted(long roleId) 
        {
            return await roleServices.MarkDeletedAsync(roleId);
        }
        /// <summary>
        /// 根据角色id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetById(long id) 
        {
            return await roleServices.GetByIdAsync(id);
        }
        /// <summary>
        /// 根据角色名字获取
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public async  Task<ApiResult> GetByName(string name) 
        {
            return await roleServices.GetByNameAsync(name);
        }
        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetAll() 
        {
            return await roleServices.GetAllAsync();
        }
        /// <summary>
        /// 给用户adminuserld 增加角色roleIds
        /// </summary>
        /// <param name="adminUserId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddRoleIds(long adminUserId, long[] roleIds) 
        {
            return await roleServices.AddRoleIdsAsync(adminUserId, roleIds);
        }
        /// <summary>
        /// 更新角色，先删再加
        /// </summary>
        /// <param name="adminUserId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateRoleIds(long adminUserId, long[] roleIds) 
        {
            return await roleServices.UpdateRoleIdsAsync(adminUserId, roleIds);
        }
        /// <summary>
        /// 获取用户的角色
        /// </summary>
        /// <param name="adminUserId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetByAdminUserId(long adminUserId) 
        {
            return await roleServices.GetByAdminUserIdAsync(adminUserId);
        }
    }
}
